<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Blog
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: Blog.php 7481 2010-09-27 08:41:01Z john $
 * @author     Jung
 */

/**
 * @category   Application_Extensions
 * @package    Blog
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Blog_Model_Blog extends Core_Model_Item_Abstract {
	// Properties
	

	protected $_parent_type = 'user';
	
	//protected $_owner_type = 'user';
	

	protected $_searchTriggers = array ('title', 'body', 'search' );
	
	protected $_parent_is_owner = true;
	
	// General
	

	/**
	 * Gets an absolute URL to the page to view this item
	 *
	 * @return string
	 */
	public function getHref($params = array()) {
		$slug = $this->getSlug ();
		
		$params = array_merge ( array ('route' => 'blog_entry_view', 'reset' => true, 'user_id' => $this->owner_id, 'blog_id' => $this->blog_id, 'slug' => $slug ), $params );
		$route = $params ['route'];
		$reset = $params ['reset'];
		unset ( $params ['route'] );
		unset ( $params ['reset'] );
		return Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( $params, $route, $reset );
	}
	
	public function getDescription() {
		// @todo decide how we want to handle multibyte string functions
		$tmpBody = strip_tags ( $this->body );
		return (Engine_String::strlen ( $tmpBody ) > 255 ? Engine_String::substr ( $tmpBody, 0, 255 ) . '...' : $tmpBody);
	}
	
	public function getKeywords($separator = ' ') {
		$keywords = array ();
		foreach ( $this->tags ()->getTagMaps () as $tagmap ) {
			$tag = $tagmap->getTag ();
			$keywords [] = $tag->getTitle ();
		}
		
		if (null === $separator) {
			return $keywords;
		}
		
		return join ( $separator, $keywords );
	}
	
	public function getSlug() {
		$str = $this->getTitle ();
		if (strlen ( $str ) > 32) {
			$str = Engine_String::substr ( $str, 0, 32 ) . '...';
		}
		$str = preg_replace ( '/([a-z])([A-Z])/', '$1 $2', $str );
		$str = strtolower ( $str );
		$str = preg_replace ( '/[^a-z0-9-]+/i', '-', $str );
		$str = preg_replace ( '/-+/', '-', $str );
		$str = trim ( $str, '-' );
		if (! $str) {
			$str = '-';
		}
		return $str;
	}
	
	// Interfaces
	

	/**
	 * Gets a proxy object for the comment handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function comments() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'comments', 'core' ) );
	}
	
	/**
	 * Gets a proxy object for the like handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function likes() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'likes', 'core' ) );
	}
	
	/**
	 * Gets a proxy object for the tags handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function tags() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'tags', 'core' ) );
	}
}